package com.itany.netclass.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.mvc.annotation.RequestMapping;
import com.itany.mvc.annotation.ResponseBody;
import com.itany.mvc.util.CommonsMultipartFile;
import com.itany.netclass.constant.PageConstant;
import com.itany.netclass.constant.ResponseCodeConstant;
import com.itany.netclass.entity.Course;
import com.itany.netclass.entity.CourseType;
import com.itany.netclass.entity.SearchForm;
import com.itany.netclass.entity.User;
import com.itany.netclass.exception.*;
import com.itany.netclass.factory.ObjectFactory;
import com.itany.netclass.service.CourseService;
import com.itany.netclass.util.ParameterUtil;
import com.itany.netclass.util.ResponseResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

;

/**
 * Author:sjy@itany.com
 * Date:21-10-14 下午6:20
 * Version:1.0-SNAPSHOT
 * Description:
 */
@RequestMapping("/course")
public class CourseController {
    @RequestMapping("/findAllType")
    public String findAllType(HttpServletRequest request, HttpServletResponse response){
        User user= (User) request.getSession().getAttribute("loginUser");
        CourseService courseService = (CourseService) ObjectFactory.getObject("CourseService");
        try {
            List<CourseType> list=courseService.findAllType(user);
            request.getSession().setAttribute("courseTypeList",list);
            return "/course/findAllCourse.do";
        } catch (NoLoginException e) {
            e.printStackTrace();
            return "/showLogin.do";
        } catch (ServiceException e) {
            e.printStackTrace();
            return "/showCourse.do";
        }
    }
    @RequestMapping("/findAllCourse")
    public String findAllCourse(HttpServletRequest request,HttpServletResponse Response){
        System.out.println("查询所有的方法");
        //第一次进入清空条件，不然会出错
        request.getSession().setAttribute("courseSearchForm",new SearchForm());

        CourseService courseService= (CourseService) ObjectFactory.getObject("CourseService");
        String pageNo = request.getParameter("pageNo");
        String pageSize =request.getParameter("pageSize");
        if (pageNo==null){
            pageNo=PageConstant.PAGE_KAISHI_NO;

        }
        if(pageSize==null){
            pageSize=PageConstant.PAGE_SIZE;
        }
        System.out.println("pageNo="+pageNo);
        try {
            PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
            List<Course> list= courseService.findAllCourse();
            PageInfo<Course> pageInfo = new PageInfo<>(list);
            request.getSession().setAttribute("courseList",pageInfo);
            request.getSession().setAttribute("NULL",null);
            return "/showCourse.do";
        } catch (ServiceException e) {
            e.printStackTrace();
            return "/showCourse.do";
        }
    }
    @ResponseBody
    @RequestMapping("/jianchaCourseName")
    public ResponseResult jianchaCourseName(HttpServletRequest request, HttpServletResponse response){
        ResponseResult result=new ResponseResult();
        CourseService courseService= (CourseService) ObjectFactory.getObject("CourseService");
        String courseId=request.getParameter("courseId");
        String courseName = request.getParameter("courseName");
        System.out.println("courseName="+courseName);
        String courseType = request.getParameter("courseType");
        System.out.println("courseType="+courseType);
        try {
            courseService.jianchaCourseName(courseId,courseName,courseType);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (NullSringException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (CourseExistException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/jianchaCourseInfo")
    public  ResponseResult jianchaCourseInfo(HttpServletRequest request,HttpServletResponse response){
        ResponseResult result=new ResponseResult();
        String courseInfo = request.getParameter("courseInfo");
        CourseService courseService= (CourseService) ObjectFactory.getObject("CourseService");
        try {
            courseService.jianchaCourseInfo(courseInfo);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (NullSringException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (ServiceException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/jianchaCourseAuthor")
    public  ResponseResult jianchaCourseAuthor(HttpServletRequest request,HttpServletResponse response){
        ResponseResult result=new ResponseResult();
        CourseService courseService= (CourseService) ObjectFactory.getObject("CourseService");
        String courseAuthor = request.getParameter("courseAuthor");
        try {
            courseService.jianchaCourseAuthor(courseAuthor);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (NullSringException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (ServiceException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/jianchaCourseTuijian")
    public  ResponseResult jianchaCourseTuijian(HttpServletRequest request,HttpServletResponse response){
        ResponseResult result=new ResponseResult();
        CourseService courseService= (CourseService) ObjectFactory.getObject("CourseService");
        String courseTuijian = request.getParameter("courseTuijian");
        try {
            courseService.jianchaCourseTuijian(courseTuijian);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (NullSringException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (ServiceException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/jianchaCourseType")
    public  ResponseResult jianchaCourseType(HttpServletRequest request,HttpServletResponse response){
        ResponseResult result=new ResponseResult();
        CourseService courseService= (CourseService) ObjectFactory.getObject("CourseService");
        String courseType = request.getParameter("courseType");
        String courseName = request.getParameter("courseName");
        String courseId = request.getParameter("courseId");
        try {
            courseService.jianchaCourseType(courseId ,courseType,courseName);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (NullSringException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (CourseExistException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/jianchaCourseFile")
    public  ResponseResult jianchaCourseFile(HttpServletRequest request,HttpServletResponse response,List<CommonsMultipartFile> list){
        ResponseResult result=new ResponseResult();
        String courseId = request.getParameter("course_id");
        System.out.println("集合的长度"+list.size());
        System.out.println("文件名"+list.get(0).getOriginalFilename());
        CourseService courseService= (CourseService) ObjectFactory.getObject("CourseService");
        try {
            courseService.jianchaCourseFile(courseId,request.getSession(),list);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (NullSringException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (TypeExistException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/addCourse")
    public  ResponseResult addCourse(HttpServletRequest request,HttpServletResponse response,List<CommonsMultipartFile> list){
        ResponseResult result=new ResponseResult();
        CourseService courseService= (CourseService) ObjectFactory.getObject("CourseService");
        String courseId= request.getParameter("course_id");
        System.out.println(courseId);
        String courseName = request.getParameter("course_name");
        System.out.println(courseName);
        String courseInfo = request.getParameter("course_info");
        System.out.println(courseInfo);
        String author = request.getParameter("author");
        System.out.println(author);
        String tuijian = request.getParameter("recommendation_grade");
        System.out.println(tuijian);
        String type = request.getParameter("course_type_id");
        System.out.println(type);
        System.out.println("集合的长度"+list.size());
        System.out.println("文件名"+ ParameterUtil.isNull(list.get(0).getOriginalFilename()));
        try {
            courseService.addCourse(courseId,courseName,courseInfo,author,tuijian,type,list,request.getSession());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/findCourseById")
    public  ResponseResult findCourseById(HttpServletRequest request,HttpServletResponse response){
        System.out.println("修改前查询的方法");
      ResponseResult result=new ResponseResult();
        String id = request.getParameter("id");
        CourseService courseService= (CourseService) ObjectFactory.getObject("CourseService");
        try {
            Course course=courseService.findCourseById(id);
            result.setReturnObject(course);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (ServiceException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/modifyCourseById")
    public  ResponseResult modifyCourseById(HttpServletRequest request,HttpServletResponse response,List<CommonsMultipartFile> list){
        System.out.println("修改的方法");
      ResponseResult result=new ResponseResult();
        CourseService courseService= (CourseService) ObjectFactory.getObject("CourseService");
        String courseId= request.getParameter("course_id");
        System.out.println(courseId);
        String courseName = request.getParameter("course_name");
        System.out.println(courseName);
        String courseInfo = request.getParameter("course_info");
        System.out.println(courseInfo);
        String author = request.getParameter("author");
        System.out.println(author);
        String tuijian = request.getParameter("recommendation_grade");
        System.out.println(tuijian);
        String type = request.getParameter("course_type_id");
        System.out.println(type);
        System.out.println("集合的长度"+list.size());
        System.out.println("文件名"+list.get(0).getOriginalFilename());
        try {
            courseService.modifyCourseById(courseId,courseName,courseInfo,author,tuijian,type,request.getSession(),list);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (Exception e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/yongCourseById")
    public  ResponseResult yongCourseById(HttpServletRequest request,HttpServletResponse response){
        ResponseResult result=new ResponseResult();
        CourseService courseService = (CourseService) ObjectFactory.getObject("CourseService");
        String courseId = request.getParameter("id");
        String status = request.getParameter("status");
        System.out.println("courseId="+courseId);
        System.out.println("status="+status);
        try {
            courseService.yongCourseById(courseId,status);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (NoQuanXianException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @RequestMapping("/sreachCourseByForm")//用于动态查询之后的分页
    public  String sreachCourseByForm(HttpServletRequest request,HttpServletResponse response){
        System.out.println("动态查询的方法");
        CourseService courseService= (CourseService) ObjectFactory.getObject("CourseService");
        String author = request.getParameter("author");
        System.out.println("author ="+author);
        String courseName = request.getParameter("courseName");
        System.out.println("courseName="+courseName);
        String status = request.getParameter("course-status-search");
        System.out.println("statsus="+status);
        String type = request.getParameter("course_type_id-search");
        System.out.println("type="+type);
        String kaishitime = request.getParameter("kaishitime");
        System.out.println("kaishitime="+kaishitime);
        String jieshutime = request.getParameter("jieshutime");
        System.out.println("jieshutime="+jieshutime);

        //以下操作为把动态查询的条件放到session中，保证页面切换后对象满足条件不变
        SearchForm searchForm = new SearchForm();
        searchForm.setCourseName(courseName);
        searchForm.setStatus(status);
        searchForm.setCourseType(type);
        searchForm.setKaishitime(kaishitime);
        searchForm.setJiesutime(jieshutime);
        searchForm.setCourseAuthor(author);
        request.getSession().setAttribute("courseSearchForm",searchForm);
        try {
            PageHelper.startPage(Integer.parseInt(PageConstant.PAGE_KAISHI_NO),Integer.parseInt(PageConstant.PAGE_SIZE));
            List<Course> list=courseService.sreachCourseByForm(author,courseName,status,type,kaishitime,jieshutime);
            PageInfo<Course> pageInfo=new PageInfo<>(list);
            //pageNo2在第一次查询所有时没有意义，作用是用来保证通过别的按钮返回当前页时留在当前页
            request.getSession().setAttribute("qianyiye",null);
            request.getSession().setAttribute("NULL",null);
            request.getSession().setAttribute("courseList",pageInfo);
            return "/showCourse.do";
        } catch (ServiceException e) {
            e.printStackTrace();
            return "/course/findAllType.do";
        } catch (NullSringException e) {
            request.getSession().setAttribute("courseList",new PageInfo<>());
            request.getSession().setAttribute("NULL","未查询到任何结果");
            return "/showCourse.do";
        }
    }

    @RequestMapping("/sreachCourseByForm2")//用于动态查询之后的分页
    public  String sreachCourseByForm2(HttpServletRequest request,HttpServletResponse response){

        System.out.println("动态sql分页的方法");
        //把之前的条件取出来进行查询
        CourseService courseService= (CourseService) ObjectFactory.getObject("CourseService");

        SearchForm searchForm = (SearchForm) request.getSession().getAttribute("courseSearchForm");
        String author= searchForm.getCourseAuthor();
        String courseName=searchForm.getCourseName();
        String status =searchForm.getStatus();
        String type = searchForm.getCourseType();
        String kaishitime = searchForm.getKaishitime();
        String jieshutime = searchForm.getJiesutime();
        System.out.println(author);
        System.out.println(courseName);
        System.out.println(status);
        System.out.println(type);
        System.out.println(kaishitime);
        System.out.println(jieshutime);
        String pageNo=null;//响应后显示的页数
        String pageNo1 = request.getParameter("pageNo");//get传过来的页数

        //pageNo2在第一次查询所有时没有意义，作用是用来保证通过别的按钮返回当前页时留在当前页
        PageInfo<Course> Info = (PageInfo<Course>) request.getSession().getAttribute("courseList");
        String pageNo2="";
        if(Info!=null){
            pageNo2=""+Info.getPageNum();
        }

        //这个是表示在找到结果是如果数据改了，导致页面变少，自动跳转到前一页的页数
        String pageNo3 = (String) request.getSession().getAttribute("qianyiye");
        System.out.println("pageNo1="+pageNo1);
        System.out.println("pageNo2="+pageNo2);
        System.out.println("pageNo3="+pageNo3);


        //以下为使用哪一个作为当前页数的判断
        if(pageNo3!=null){
            System.out.println("用了前一页");
            pageNo=pageNo3;
        }else if(pageNo1!=null){
            System.out.println("用了get");
            pageNo=pageNo1;
        }else if(!ParameterUtil.isNull(pageNo2)){
            System.out.println("用了session");
            pageNo=pageNo2;
        }else {
            System.out.println("用了常量");
            pageNo=PageConstant.PAGE_KAISHI_NO;
        }

        String pageSize = request.getParameter("pageSize");
        if(pageSize==null){
            pageSize=PageConstant.PAGE_SIZE;
        }
        try {
            PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
            List<Course> list=courseService.sreachCourseByForm(author,courseName,status,type,kaishitime,jieshutime);
            PageInfo<Course> pageInfo=new PageInfo<>(list);

//            查到了结果清空存放前一页和五错误对象的session，不然下次查询或页面切换可能会出错
            request.getSession().setAttribute("qianyiye",null);
            request.getSession().setAttribute("NULL",null);
            request.getSession().setAttribute("courseList",pageInfo);
            return "/showCourse.do";
        } catch (ServiceException e) {
            e.printStackTrace();
            return "/course/findAllType.do";
        } catch (NullSringException e) {//表示当前页数无任何数据的异常需要跳转
            if(pageNo.equals(PageConstant.PAGE_KAISHI_NO)){
                //已经是第一页，表示为无满足条件的对象
                request.getSession().setAttribute("courseList",new PageInfo<>());
                request.getSession().setAttribute("NULL","未查询到任何结果");
                return "/showCourse.do";
            }else {
                //设置前一页的页码放到session中
                String pageNum = ""+(Info.getPageNum()-1);
                request.getSession().setAttribute("qianyiye",pageNum);
                return "/course/sreachCourseByForm2.do";
            }
        }
    }
}
